﻿(function ($, window, undefined) {
    if ($ === undefined) {
        $ = window;
    }
    // 序列化为对象
    $.serializeObj = function (selector) {
        var array = $(selector).find('input');//$(selector).serializeArray();
        var array2 = $(selector).find('select');
        var array3 = $(selector).find('textarea');
        var obj = {};
        setFormItemValue(obj, array);
        setFormItemValue(obj, array2);
        setFormItemValue(obj, array3);
        return obj;
    };
    function setFormItemValue(obj, array) {
        for (var i = 0; i < array.length; ++i) {
            if (obj[array[i]['name']]) {
                continue;
            }
            if (array[i]['type'] === 'checkbox') {
                obj[array[i]['name']] = array[i]['checked'];
            } else {
                obj[array[i]['name']] = array[i]['value'];
            }
            //console.log(array[i]['name'] + '=' + array[i]['value']);
        }
        return obj;
    }
    // 序列化为formdata
    $.serializeForm = function (selector) {
        var array = $(selector).find('input');//$(selector).serializeArray();
        var array2 = $(selector).find('select');
        var obj = new FormData();
        setFormDataValue(obj, array);
        setFormDataValue(obj, array2);
        return obj;
    };
    function setFormDataValue(obj, array) {
        for (var i = 0; i < array.length; ++i) {
            if (obj[array[i]['name']]) {
                continue;
            }
            if (array[i]['type'] === 'file') {
                var files = array[i]['files'];
                if (files.length > 1) {
                    for (var i = 0; i < files.length; ++i) {
                        obj.append(array[i]['name'] + i, files[i]);
                    }
                } else {
                    obj.append(array[i]['name'], files[0]);
                }
            } else {
                obj.append(array[i]['name'], array[i]['value']);
            }
        }
        return obj;
    }
    // 序列化表行中项为对象数组
    $.serializeTableObj = function (selector) {
        var trs = $(selector);
        var rows = [];
        for (var i = 0; i < trs.length; ++i) {
            var $tr = $(trs[i]);
            var array = $tr.find('input');//$(selector).serializeArray();
            var array2 = $tr.find('select');
            var array3 = $tr.find('textarea');
            var obj = {};
            setFormItemValue(obj, array);
            setFormItemValue(obj, array2);
            setFormItemValue(obj, array3);
            rows.push(obj);
        }

        return rows;
    };

    $.showTableObj = function (selector, obj) {
        var trs = $(selector);
        var sets = [];
        for (var i = 0; i < trs.length; ++i) {
            var $tr = $(trs[i]);
            for (var key in obj) {
                if (key in sets) {
                    continue;
                }
                var ele = $tr.find('[name=' + key + ']');
                if (ele === undefined || ele.length === 0) {
                    continue;
                }
                $(ele[0]).val(obj[key]);
            }
        }
    };

    function setArrayItemValue(arr, array, isdistinct) {
        isdistinct = !!isdistinct;
        var val;
        for (var i = 0; i < array.length; ++i) {            
            if (array[i]['type'] === 'checkbox') {
                val = array[i]['checked'];
            } else {
                val = array[i]['value'];
            }
            val = val || '';
            if (val === '') {
                continue;
            }
            if (isdistinct && arr.findIndex(n => n === val) !== -1) {
                continue;
            }
            arr.push(val);
        }
        return arr;
    }
    // 序列化指定名称的项为数组
    $.serializeNameArray = function (selector,name,isdistinct) {
        var array = $(selector).find('input[name='+name+']');//$(selector).serializeArray();
        var array2 = $(selector).find('select[name=' + name +']');
        var array3 = $(selector).find('textarea[name=' + name +']');
        var arr = [];

        setArrayItemValue(arr, array, isdistinct);
        setArrayItemValue(arr, array2, isdistinct);
        setArrayItemValue(arr, array3, isdistinct);
        return arr;
    };
})(jQuery, window, undefined);